﻿package com.victim.snake.graphics.mainClass {
	import flash.display.Graphics;
	import flash.display.Sprite;
	import flash.display.Bitmap;
	import com.victim.snake.util.Loader;
	import flash.events.Event;
	import flash.display.LoaderInfo;
	import com.victim.snake.graphics.mainClass.RoomGridItem;
	import flash.events.MouseEvent;
	import com.victim.snake.room.Room;
	
	
	//房间列表类	
	public class RoomGrid extends Sprite{
		
		var currentItem:int=0;	
		var roomGridItemArray:Array=new Array();	//房间列表项数组
		private var loadingState:int=0;	//加载状态  （0 以加载   1 等待加载    用调用MainSprite.requestRoomList方法时，状态为等待加载，当接收到第一条房间信息后，回复以加载状态）
		
		
		//背景/鼠标经过时/按下时/按下经过时
		var roomGridItemBackground:Bitmap=null;
		var roomGridItemOnMouseOverBackground:Bitmap=null;	
		var roomGridItemOnSelectBackgroundDown:Bitmap=null;	
		var roomGridItemSelectOnMouseOverBackground:Bitmap=null;
		
		
		
		public function RoomGrid():void{			
		
			roomGridItemArray[0]=new RoomGridItem(216,100,this);
			this.addChildAt(roomGridItemArray[0],0);
			roomGridItemArray[1]=new RoomGridItem(496,100,this);
			this.addChildAt(roomGridItemArray[1],1);
			roomGridItemArray[2]=new RoomGridItem(216,167,this);
			this.addChildAt(roomGridItemArray[2],2);
			roomGridItemArray[3]=new RoomGridItem(496,167,this);
			this.addChildAt(roomGridItemArray[3],3);
			roomGridItemArray[4]=new RoomGridItem(216,234,this);
			this.addChildAt(roomGridItemArray[4],4);
			roomGridItemArray[5]=new RoomGridItem(496,234,this);
			this.addChildAt(roomGridItemArray[5],5);
		
			//下载房间列表项背景
			var roomGridItemBackgroundLoader:Loader = new Loader("image/main/roomGridItemBackground.png",roomGridItemBackgroundLoaderEvent);
			var roomGridItemBackgroundDownLoader:Loader = new Loader("image/main/roomGridItemBackgroundDown.png",roomGridItemOnSelectBackgroundDownEvent);
			var roomGridItemOnMouseOverBackgroundLoader:Loader = new Loader("image/main/roomGridItemOnMouseOverBackground.png",roomGridItemOnMouseOverBackgroundEvent);
			var roomGridItemSelectOnMouseOverBackgroundLoader:Loader = new Loader("image/main/roomGridItemSelectOnMouseOverBackground.png",roomGridItemSelectOnMouseOverBackgroundEvent);
			
			
			
			
			
		}
		
		//初使化列表
		public function init():void{
			currentItem=0;
			RoomGridItem(roomGridItemArray[0]).init();
			RoomGridItem(roomGridItemArray[1]).init();
			RoomGridItem(roomGridItemArray[2]).init();
			RoomGridItem(roomGridItemArray[3]).init();
			RoomGridItem(roomGridItemArray[4]).init();
			RoomGridItem(roomGridItemArray[5]).init();
		}

		
		//选择状态初使化
		public function selectInit():void{
			RoomGridItem(roomGridItemArray[0]).selectInit();
			RoomGridItem(roomGridItemArray[1]).selectInit();
			RoomGridItem(roomGridItemArray[2]).selectInit();
			RoomGridItem(roomGridItemArray[3]).selectInit();
			RoomGridItem(roomGridItemArray[4]).selectInit();
			RoomGridItem(roomGridItemArray[5]).selectInit();
		}
		
		public function addItem(room:Room):void{
			if(this.loadingState==1){
				trace("房间列表数据以下载");
				this.init();
				this.setLoadingState(false);		//设置为以加载
			}
			//取得当前列表项
			var rgi:RoomGridItem=RoomGridItem(roomGridItemArray[currentItem]);			
			rgi.addRoom(room);
			this.currentItem+=1;
		}

		//当列表项背景下载完成，给列表项设置背景
		private function roomGridItemBackgroundLoaderEvent(event:Event):void{			
			var loaderInfo:LoaderInfo=LoaderInfo(event.target);
			this.roomGridItemBackground=Bitmap(loaderInfo.content);
			//给列表项设置背景
			for(var i:int=0;i<roomGridItemArray.length;i++){
				var r:RoomGridItem=RoomGridItem(roomGridItemArray[i]);
				r.roomGridItemBackground=new Bitmap(this.roomGridItemBackground.bitmapData.clone());
				r.addChildAt(r.roomGridItemBackground,0);
				r.selectInit();
			}
		}
		
		//当列表项按下背景下载完成
		private function roomGridItemOnSelectBackgroundDownEvent(event:Event):void{			
			var loaderInfo:LoaderInfo=LoaderInfo(event.target);
			roomGridItemOnSelectBackgroundDown=Bitmap(loaderInfo.content);
			
			//给列表项设置背景
			for(var i:int=0;i<roomGridItemArray.length;i++){
				var r:RoomGridItem=RoomGridItem(roomGridItemArray[i]);
				r.roomGridItemOnSelectBackgroundDown=new Bitmap(this.roomGridItemOnSelectBackgroundDown.bitmapData.clone());
			}
		}
		
		//当列表项鼠标停留背景下载完成
		private function roomGridItemOnMouseOverBackgroundEvent(event:Event):void{			
			var loaderInfo:LoaderInfo=LoaderInfo(event.target);
			roomGridItemOnMouseOverBackground=Bitmap(loaderInfo.content);
			
			//给列表项设置背景
			for(var i:int=0;i<roomGridItemArray.length;i++){
				var r:RoomGridItem=RoomGridItem(roomGridItemArray[i]);
				r.roomGridItemOnMouseOverBackground=new Bitmap(this.roomGridItemOnMouseOverBackground.bitmapData.clone());
			}
		}
		
		//当列表项按下鼠标停留背景下载完成
		private function roomGridItemSelectOnMouseOverBackgroundEvent(event:Event):void{			
			var loaderInfo:LoaderInfo=LoaderInfo(event.target);
			this.roomGridItemSelectOnMouseOverBackground=Bitmap(loaderInfo.content);
			
			//给列表项设置背景
			for(var i:int=0;i<roomGridItemArray.length;i++){
				var r:RoomGridItem=RoomGridItem(roomGridItemArray[i]);
				r.roomGridItemSelectOnMouseOverBackground=new Bitmap(this.roomGridItemSelectOnMouseOverBackground.bitmapData.clone());
			}
		}
		
		
		public function setLoadingState(b:Boolean):void{
			b?this.loadingState=1:this.loadingState=0;
		}
	} 
	
}
